layui.use(['table', 'jquery'], function (data) {
    var table = layui.table,
        $ = layui.jquery;

    var userName = $('input[name="userName"]').val();
    var isLeader = $('input[name="leader"]').val();
    var tableIns = table.render({
        title: "待质检订单",
        elem: "#qualityList",
        height: 'full-125',
        url: ctx + '/quality/selectByParamsWithPurchase',
        page: true,
        cellMinWidth: 80, // 单元格最小宽度
        toolbar: '#toolbarDemo',
        cols: [[// 表头
            {type: 'checkbox'},
            {type: "numbers"},
            {
                title: "采购订单", templet: function (data) {
                    return data.purchaseRequestBean.purchaseNo;
                }
            },
            {
                title: "物料编码", templet: function (data) {
                    return data.purchaseRequestBean.materialNo;
                }
            },
            {
                title: "物料名称", templet: function (data) {
                    return data.purchaseRequestBean.materialName;
                }
            },
            {
                title: "物料数量", templet: function (data) {
                    return data.purchaseRequestBean.purchaseCount;
                }
            },
            {
                title: "供应商名称", templet: function (data) {
                    return data.purchaseRequestBean.supplierBean.supplierName;
                }
            },
            {
                title: "采购时间", templet: function (data) {
                    return data.purchaseRequestBean.purchaseCreateDate
                }
            },
            {
                title: "订单类型", field: "qualityType", templet: function (data) {
                    if (data.qualityType === 0) {
                        return "原材料购买";
                    }
                    if (data.qualityType === 1) {
                        return "生产补料";
                    }
                }
            },
            {
                title: "状态", field: "qualityStatus", templet: function (data) {
                    if (data.qualityStatus == 0) {
                        return "未指派"
                    }
                    if (data.qualityStatus == 1) {
                        return "质检中"
                    }
                    if (data.qualityStatus == 2) {
                        return "已完成"
                    }
                }
            }

            , {
                title: "质检员", templet: function (data) {
                    if (data.qualityerId == null) {
                        return "未指派"
                    } else {
                        return data.empBean.empName
                    }
                }
            },
            {
                title: "操作", templet: function (data) {
                    if (data.qualityStatus == 0 && isLeader == 1) {
                        return '<a  class="layui-btn  layui-btn-xs" lay-event="assign">指派</a>';
                    }
                    if (data.empBean != null) {
                        if (data.empBean.empName.localeCompare(userName) == 0) {
                            return '<a  class="layui-btn layui-btn-primary  layui-btn-xs" lay-event="quality">质检</a>';
                        } else {
                            return '<a visibility="hidden"     disabled=""  lay-event=""></a>';
                        }
                    } else {
                        return '<a visibility="hidden"     disabled=""  lay-event=""></a>'
                    }
                }
            }
        ]]
    });
    // 监听头
    // table.on('toolbar(qualityFilter)', function (data) {
    //     if (data.event == 'assign') {
    //         var checkStatus = table.checkStatus(data.config.id);
    //         openAssignEmpToQuality(checkStatus.data);
    //     }
    // });
    table.on('tool(qualityFilter)', function (data) {
        if (data.event === "quality") {
            openQualityDialog(data.data);
        }
        if (data.event == "assign") {
            openAssignEmpToQuality(data.data);
        }
    })

    function openAssignEmpToQuality(data) {
        console.log(data)
        if (data.qualityStatus != 0) {
            layer.msg("已经指派过人员了", {icon: 6})
            return;
        }
        layer.open({
            type: 2,
            title: "质检---指派质检员",
            area: ['700px', '500px'],
            content: ctx + "/quality/toAssignPage?id=" + data.id,
            shadeClose: true,//点击遮罩关闭层
            maxmin: true
        })
    };

    function openQualityDialog(data) {
        console.log(data)
        layer.open({
            type: 2,
            title: "质检---质检结果",
            area: ['700px', '500px'],
            content: ctx + "/quality/toAddQualityResultPage?id=" + data.id,
            shadeClose: true,//点击遮罩关闭层
            maxmin: true
        })
    }
})